<template>
  <div class="systemConfig">
    <h3>系统配置</h3>
    <div class="box">
      <div class="boxItem" v-for="(item,index) in dataList" :key="index">
        <div class="row" v-if="item.tag_name=='radio'">
          <span>{{item.name}}：</span>
          <el-radio-group v-model="item.svalue">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </div>
        <div class="row2" v-if="item.tag_name=='input'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-input v-model="item.svalue" placeholder="请输入" clearable item.tag_attr></el-input>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
        <div class="row2" v-if="item.tag_name=='textarea'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-input v-model="item.svalue" placeholder="请输入内容" clearable type="textarea" :rows="3"></el-input>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
        <div class="row2" v-if="item.tag_name=='select'">
          <div class="row">
            <span>{{item.name}}：</span>
            <el-select v-model="item.svalue" placeholder="请选择" item.tag_attr>
              <el-option
                v-for="item in item.tag_vlist_arry"
                :key="item.label"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <div class="ps" v-if="item.remark">{{item.remark}}</div>
        </div>
      </div>
      <div class="boxItem">
        <div class="row">
          <span></span>
          <el-button type="primary" @click="saveInfo">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "systemConfig",
  data() {
    return {
      initData: {},
      dataList: []
    };
  },
  mounted() {
    this.getSystemConfigInfo();
  },
  methods: {
    //获取系统配置详情
    async getSystemConfigInfo() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.systemConfigInfo,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        Object.keys(this.initData).forEach(key => {
          this.dataList.push(this.initData[key]);
        });
        console.log(this.dataList);
        //当是select的时候，转换tag_vlist的格式
        this.dataList.forEach((item, index) => {
          if (item.tag_name == "select") {
            let objList = [];
            Object.keys(JSON.parse(item.tag_vlist)).forEach((it, idx) => {
              let obj = {};
              obj.label = JSON.parse(item.tag_vlist)[it];
              obj.value = it;
              objList.push(obj);
            });
            item.tag_vlist_arry = objList;
            console.log(item.tag_vlist_arry);
          }
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //保存配置信息
    async saveInfo() {
      let obj = {};
      this.dataList.forEach((item, index) => {
        obj[item.skey] = item.svalue;
      });
      let parames = {
        handshake: "",
        data: obj
      };
      let res = await this.$axios.post(
        this.$api.systemConfigSave,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: "保存成功",
          type: "success"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.systemConfig {
  width: 100%;
  padding-bottom: 0.3rem;
}
.box {
  width: 100%;
  display: flex;
  /* justify-content: center; */
  flex-direction: column;
  align-items: center;
}
.row {
  width: 100%;
  display: flex;
  align-items: center;
}
.ps {
  width: 80%;
  text-align: left;
  padding-left: 2rem;
  color: #ca674c;
}
.row span {
  display: flex;
  width: 2rem;
}
.row2 {
  width: 100%;
}
.row2 .row {
  width: 100%;
}
.boxItem {
  width: 80%;
  margin-top: 0.2rem;
}
.el-select {
  width: 100%;
}
</style>